package coding;

public class Code01 {
    public static void main(String[] args) {
        String str = "刘Ab .·- &*";
        System.out.println(fun(str));
    }

    /**
     * 姓名输入校验
     * 仅支持汉字,英文字母,' ','.','·','-'
     */
    private static String fun(String str) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            if (chineseCharacterValidate(ch) || Character.isLetter(ch) || ch == ' '
                    || ch == '.' || ch == '·' || ch == '-') {
                sb.append(ch);
            }
        }
        return sb.toString();
    }

    /**
     * 中文字符校验
     */
    private static boolean chineseCharacterValidate(char ch) {
        try {
            byte[] bytes = String.valueOf(ch).getBytes("Unicode");
            String unicodeStr = new String(bytes, "Unicode");
            String regex = "^[\\u4e00-\\u9fa5]+$";
            return unicodeStr.matches(regex);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return false;
    }
}
